package com.example.demo.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.example.demo.interceptor.AccessControlAllowOriginInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Author x c
 * @Email 378661553@qq.com
 * @Date Created in 2019/12/19 17:21
 */
@SpringBootConfiguration
public class WebMvcConfig implements WebMvcConfigurer {

    @Value("${spring.profiles.include}")
    private String include;


    /**
     * 允许跨域配置
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                //允许客户端携带自定义请求头
                .allowCredentials(true)
                //域名 e.g."https://domain.com" 生产环境不允许设置为“*”
                .allowedOrigins("*")
                .allowedMethods("GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "TRACE")
                .maxAge(3600)
                .allowedHeaders();
    }

    /**
     * 添加拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new AccessControlAllowOriginInterceptor()).addPathPatterns("/**");
    }



    /**
     * SpringBoot 默认的 HttpMessageConverters 是 org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
     * 自定义 HttpMessageConverters 转换器
     * <p>
     * <p>
     * SerializerFeature.WriteNullListAsEmpty,//如果 List 类型的字段是null的就用 [] 表示
     * SerializerFeature.WriteNullStringAsEmpty,//如果 String 类型的字段是 null 就用""表示
     * SerializerFeature.DisableCircularReferenceDetect,//禁用循环引用检测
     * SerializerFeature.PrettyFormat,//对json字符串进行格式化输出
     * SerializerFeature.WriteMapNullValue, // 如果对象包含的值有null的话，也将其进行序列化，如果没有该配置那么会导致 null 的字段不会进行序列化
     * SerializerFeature.WriteDateUseDateFormat,//将java.util.Date类型的进行格式化（yyyy-MM-dd hh:mm:ss）输出,如果没有这个配置就直接输出为时间戳
     * SerializerFeature.UseISO8601DateFormat,// 将java.util.Date类型的根据 ISO8601 进行格式化：2019-12-19T15:46:34.099+08:00
     * SerializerFeature.WriteClassName,// 没什么用的配置
     * SerializerFeature.BeanToArray,//将Javabean转化为数组，没什么用的配置
     * SerializerFeature.WriteNullBooleanAsFalse,//如果Boolean类型的字段是null的就用false表示
     * SerializerFeature.WriteNullNumberAsZero//如果Number类型的字段是null的就用0表示
     */
    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverter() {
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        SerializerFeature[] prod = {SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteMapNullValue};
        SerializerFeature[] dev = {SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteMapNullValue, SerializerFeature.PrettyFormat};
        boolean isDev = include != null && include.contains("dev");
        fastJsonConfig.setSerializerFeatures(isDev ? dev : prod);
        fastConverter.setFastJsonConfig(fastJsonConfig);
        return new HttpMessageConverters(fastConverter);
    }

}
